package com.hyyt.shopjsp.finance.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.export.ExportExcel;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.finance.pojo.ShopMallCoin;
import com.hyyt.shopjsp.finance.service.IShopMallCoinService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/4/01 0024上午 12:20.
 */
@Controller
@RequestMapping("/back/mallCoin")
public class ShopMallCoinController extends BaseController {
	private Logger log = Logger.getLogger(ShopMallCoinController.class);
	private String basePath = "/back/shop/mallCoin/";
	
	@Resource
	private IShopMallCoinService shopMallCoinService;//金币明细service
	/**
	 * 金币明细列表
	 * @param customerId
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="customerMallCoinList",method=RequestMethod.POST)
	@ResponseBody
	public void customerMallCoinList(String customerId,String serialNumber,String type,Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object>param=new HashMap<>();
		if(Utils.objectIsNotEmpty(serialNumber)){
			param.put("serialNumber", serialNumber.trim());
		}
		if(Utils.objectIsNotEmpty(type)){
			param.put("type", type);
		}
		param.put("customerId", customerId);
		int totalRecordCount =shopMallCoinService.countShopMallCoin(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopMallCoin> mallCoinList =shopMallCoinService.queryShopMallCoin(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", mallCoinList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 获得金币明细对象
	 * @param mallCoinId
	 * @return
	 */
	@RequestMapping(value="getMallCoinObject",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getMallCoinObject(Integer mallCoinId){
		if(Utils.objectIsNotEmpty(mallCoinId)){
			ShopMallCoin shopMallCoin = shopMallCoinService.loadShopMallCoin(mallCoinId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(shopMallCoin));
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	 * 跳转会员金币
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoMallCoin")
	public String gotoMallCoin(){
        return basePath + "mallCoinList";
	}

	/**
	 * 会员金币列表
	 * @param loginName 会员名称
	 * @param ordersNo 订单号
	 * @param type 收支类型
	 * @param registerDate 交易开始时间
	 * @param registerDate2 交易结束时间
	 * @param page 分页
	 * @param response 结果集
	 */
	@RequestMapping(value="mallCoinList",method=RequestMethod.POST)
	@ResponseBody
	public void mallCoinList(String loginName, String ordersNo, String type, String registerDate, String registerDate2, Page page, HttpServletResponse response){
		Map<String, Object> param=new HashMap<>();
		if(Utils.objectIsNotEmpty(loginName)){
			param.put("loginName", loginName.trim());
		}
		if(Utils.objectIsNotEmpty(ordersNo)){
			param.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(type)){
			param.put("type", type);
		}
		if(Utils.objectIsNotEmpty(registerDate)){
			param.put("registerDate", registerDate);
		}
		if(Utils.objectIsNotEmpty(registerDate2)){
			param.put("registerDate2", registerDate2);
		}
		param.put("sortColumns", "tradeTime desc");
		int totalRecordCount =shopMallCoinService.countMallCoinList(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopMallCoin> mallCoinList=shopMallCoinService.queryMallCoinList(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", mallCoinList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
	 * 获得会员金币集合
	 * @param mallCoinId 会员金币id
	 * @return 返回集合
	 */
	@RequestMapping(value="getVirtualCoinById",method=RequestMethod.POST)
	@ResponseBody
	public ResultMsg getVirtualCoinById(Integer mallCoinId){
		Map<String, Object> param=new HashMap<>();
		if(Utils.objectIsNotEmpty(mallCoinId)){
			param.put("mallCoinId",mallCoinId);
			List<ShopMallCoin> list = shopMallCoinService.queryShopMallCoinDetail(param);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(list));
		}else {
			return ResultMsg.create(false);
		}
	}


	/**
	 * 导出会员金币excel
	 * @param loginName 会员名称
	 * @param ordersNo 订单号
	 * @param type 收支类型
	 * @param registerDate 交易开始时间
	 * @param registerDate2 交易结束时间
	 * @param request
	 * @param response 结果集
	 */
	@RequestMapping("exportMallCoinExcel")
	public void exportMallCoinExcel(String loginName, String ordersNo, String type, String registerDate, String registerDate2, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		Map<String, Object> param=new HashMap<>();
		if(Utils.objectIsNotEmpty(loginName)){
			//用于get请求强转，防止中文乱码
			loginName= new String(loginName.trim().getBytes("iso8859-1"),"UTF-8");
			param.put("loginName", loginName);
		}
		if(Utils.objectIsNotEmpty(ordersNo)){
			param.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(type)){
			param.put("type", type);
		}
		if(Utils.objectIsNotEmpty(registerDate)){
			param.put("registerDate", registerDate);
		}
		if(Utils.objectIsNotEmpty(registerDate2)){
			param.put("registerDate2", registerDate2);
		}
		param.put("sortColumns", "tradeTime desc");
		List<ShopMallCoin> mallCoinList=shopMallCoinService.queryMallCoinList(param);
		SimpleDateFormat formatter = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
		List<Map<String,Object>> mallCoin = new ArrayList<>();
		if(mallCoinList!=null&&mallCoinList.size()>0){
			for (ShopMallCoin shopMallCoin : mallCoinList) {
				Map<String,Object> map = new HashMap<>();
				map.put("serialNumber", shopMallCoin.getSerialNumber());
				map.put("loginName", shopMallCoin.getLoginName());
				map.put("ordersNo", shopMallCoin.getOrdersNo());
				map.put("transactionNumber", shopMallCoin.getTransactionNumber());
				if(shopMallCoin.getType()==1){
					map.put("type","收入");
				}else if(shopMallCoin.getType()==2){
					map.put("type","支出");
				}
				map.put("tradeTime", formatter.format(shopMallCoin.getTradeTime()));
				mallCoin.add(map);
			}
		}
		String fileName ="会员金币明细";
		String columnNames[] = {"流水号","会员名称","订单号","交易数量(个)","收支类型","交易时间"};
		String keys[] = {"serialNumber","loginName","ordersNo","transactionNumber","type","tradeTime"};
		try {
			ExportExcel.download(fileName,mallCoin,columnNames,keys,request,response);
		} catch (IOException e) {
			logger.error("导出Excel失败！");
		}
	}

}